/*
 * Copyright 2009 fearsomeJ.org
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.fearsomej.jayme.process;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

/**
 * Decisions provide procedural control to the JaymeProcess. They can be used in
 * a number of different combinations to produce looping structures, if then
 * operations, and else operations for example. Basically, a decision must be
 * evaluated to be true for a transition to execute its "toStep". If the
 * decision evaluates to false, the transition returns control to the
 * JaymeProcess which then executes the next transition or returns control to
 * the Process Client. Two decisions on the same transition is effectively the
 * same as an "and" operation, both decisions must evaluate to true for the
 * transition to execute its "toStep". Two transitions with the same "fromStep"
 * and "toStep" both containing a decision is effectively an "or" operation.
 * 
 * @author Kevin Beddingfield
 * 
 */
public class Decision {
	private String operation;
	private Language language = Language.JavaScript;

	public enum Language {
		JavaScript
	};

	public Language getLanguage() {
		return language;
	}

	public void setLanguage(Language language) {
		this.language = language;
	}

	public String getOperation() {
		return operation;
	}

	public void setOperation(String operation) {
		this.operation = operation;
	}

	// TODO manage the scripting framework life cycle better
	public Boolean evaluatesToTrue() {
		Boolean evaluation = Boolean.FALSE;
		ScriptEngineManager mgr = new ScriptEngineManager();
		ScriptEngine scriptEngine = mgr.getEngineByName(language.name());
		try {
			evaluation = (Boolean) scriptEngine.eval(operation);
		} catch (ScriptException ex) {
			throw new JaymeScriptException(ex);
		}
		return evaluation;
	}

}
